package com.zxm.sunshineaiary.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout.LayoutParams;

import com.zxm.sunshineaiary.activity.base.MybaseActivity;
import com.zxm.sunshineaiary.animation.MonthLayoutAnimation;
import com.zxm.sunshineaiary.info.DateInfo;
import com.zxm.sunshineaiary.util.CharsUtil;
import com.zxm.sunshineaiary.util.ColorUtil;
import com.zxm.sunshineaiary.view.calendar.datewidgetdaycell.DateWidgetDay;
import com.zxm.sunshineaiary.view.calendar.datewidgetdaycell.DateWidgetDayCell;
import com.zxm.sunshineaiary.view.calendar.datewidgetdaycell.DateWidgetDayCell.OnItemClick;
import com.zxm.sunshineaiary.view.flinggallery.adapter.FingGalleryAdapter;
import com.zxm.sunshineaiary.view.flinggallery.view.FingGallery;
import com.zxm.sunshineaiary.view.mydialog.DeleteDiaryDialog;

public class DateMenuActivity extends MybaseActivity implements
		OnClickListener, OnItemClickListener, OnItemClick {

	private String TAG = "DateMenuActivity";

	private TextView mMonthText = null;
	private FingGalleryAdapter mAdapter = null;
	private MonthLayoutAnimation mMonthAnimation = null;
	private ImageView mMothButton = null;
	private ImageView mBackButton = null;
	private FingGallery mGallery = null;
	private DateWidgetDay mDateWidgetDay = null;
	private DateInfo mDateInfo = DateInfo.getInstance();

	private boolean mOpenMonehState = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.date);

		findView();
		setTextColor();
		setmAdapter();
		setListener();
		initialControls();
		mMonthAnimation = createMonthAnimation();
	}

	/**
	 * 根据日期和用户资料初始化控件
	 */
	private void initialControls() {
		int month = mDateWidgetDay.getTodayMonth();
		Log.v(TAG, month + "");
		mGallery.setGalleryPosation(month);
		mMonthText.setText(month + 1 + "");
	}

	private void setmAdapter() {
		mAdapter = createAdapter(createText());
		mGallery.setAdapter(mAdapter);
	}

	private void setListener() {
		mMothButton.setOnClickListener(this);
		mBackButton.setOnClickListener(this);
		mMonthText.setOnClickListener(this);
		mGallery.setmOnItemClickListener(this);
		mDateWidgetDay.setOnItemClickListener(this);

	}

	private void findView() {
		mMonthText = (TextView) this.findViewById(R.id.date_text_month);
		mGallery = (FingGallery) this.findViewById(R.id.date_gallery);
		mMothButton = (ImageView) this.findViewById(R.id.date_button_month);
		mDateWidgetDay = (DateWidgetDay) this.findViewById(R.id.date_calendar);
		mBackButton = (ImageView) this.findViewById(R.id.date_backButton);
	}

	/**
	 * 设置字体颜色
	 */
	private void setTextColor() {
		ColorUtil.setTextColor(mMonthText);
	}

	/**
	 * 创建动画类并把需要运行动画的View加入到View[]中传递给Animation
	 * 
	 * @return
	 */
	private MonthLayoutAnimation createMonthAnimation() {
		MonthLayoutAnimation monthAnimation = null;
		View[] vs = new View[3];
		vs[0] = mGallery;
		vs[1] = mMothButton;
		vs[2] = mDateWidgetDay;
		monthAnimation = new MonthLayoutAnimation(this, vs,
				MonthLayoutAnimation.MARGIN_SHORT);
		return monthAnimation;
	}

	private TextView[] createText() {
		ColorUtil.setViewColor(mGallery);
		TextView[] mGlleryText = new TextView[12];
		for (int i = 0; i < mGlleryText.length; i++) {
			mGlleryText[i] = new TextView(this);
			mGlleryText[i].setText((i + 1) + "");
			mGlleryText[i].setTextSize(50);
			ColorUtil.setViewColor(mGlleryText[i]);
		}
		return mGlleryText;
	}

	/**
	 * 创建Adapter并返回
	 * 
	 * @param view
	 * @return
	 */
	private FingGalleryAdapter createAdapter(View[] view) {
		FingGalleryAdapter adapter = null;
		int h = CharsUtil.dipTopx(this, 60);
		adapter = new FingGalleryAdapter(this, view, LayoutParams.WRAP_CONTENT,
				h, 0);
		return adapter;
	}

	/**
	 * 控制动画展开和收缩的方法，根据mOpenMonehState来判断
	 */
	private void ClickMonthButton() {
		mMonthAnimation.setOpenAnimationState(mOpenMonehState);
		if (mOpenMonehState) {
			mMothButton.setBackgroundResource(R.drawable.right);
			mGallery.startAnimation(mMonthAnimation);
			mOpenMonehState = false;
		} else {
			mMothButton.setBackgroundResource(R.drawable.left);
			mGallery.startAnimation(mMonthAnimation);
			mOpenMonehState = true;
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP && mOpenMonehState) {
			ClickMonthButton();
			mOpenMonehState = false;
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.date_button_month:
			ClickMonthButton();
			break;
		case R.id.date_text_month:
			ClickMonthButton();
			break;
		case R.id.date_backButton:
			startNextActivity(MainActivity.class);
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		mMonthText.setText(position + 1 + "");
		int year = Integer.parseInt(mDateInfo.year);
		mDateWidgetDay.setDateWidgetDay(year, position);
	}

	@Override
	public void OnDateClick(DateWidgetDayCell item) {
		mDateInfo.month = item.iDateMonth + 1 + "";
		mDateInfo.day = item.iDateDay + "";
		startNextActivity(DiaryActivity.class);
	}

	@Override
	public void OnLongDateClick(DateWidgetDayCell item) {
		mDateInfo.month = item.iDateMonth + 1 + "";
		mDateInfo.day = item.iDateDay + "";
		new DeleteDiaryDialog(this, mDateWidgetDay,mDateInfo).show();
	}

}
